import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ProductManager productManager = new ProductManager();
        Scanner scanner = new Scanner(System.in);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        while (true) {
            System.out.println("1. 添加商品");
            System.out.println("2. 更新商品");
            System.out.println("3. 删除商品");
            System.out.println("4. 查询商品");
            System.out.println("5. 显示所有商品");
            System.out.println("6. 显示仓库总价值");
            System.out.println("7. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    System.out.println("输入商品id:");
                    int id = scanner.nextInt();
                    scanner.nextLine(); // consume newline
                    System.out.println("输入商品名称:");
                    String name = scanner.nextLine();
                    System.out.println("输入商品数量:");
                    int quantity = scanner.nextInt();
                    System.out.println("输入商品价格:");
                    double price = scanner.nextDouble();
                    System.out.println("输入商品到期日期 (yyyy-MM-dd):");
                    String dateStr = scanner.next();
                    Date expirationDate = null;
                    try {
                        expirationDate = dateFormat.parse(dateStr);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    Product product = new Product(id, name, quantity, price, expirationDate);
                    productManager.addProduct(product);
                    break;
                case 2:
                    // Similar to adding a product, prompt for product details and call updateProduct
                    break;
                case 3:
                    System.out.println("输入要删除的商品ID:");
                    int idToDelete = scanner.nextInt();
                    productManager.deleteProduct(idToDelete);
                    break;
                case 4:
                    System.out.println("输入要查询的商品ID:");
                    int idToGet = scanner.nextInt();
                    Product productToGet = productManager.getProduct(idToGet);
                    if (productToGet != null) {
                        System.out.println(productToGet);
                    } else {
                        System.out.println("商品未找到.");
                    }
                    break;
                case 5:
                    List<Product> products = productManager.getAllProducts();
                    for (Product p : products) {
                        System.out.println(p);
                    }
                    break;
                case 6:
                    double totalValue = productManager.getTotalValue();
                    System.out.println("仓库总价值: " + totalValue);
                    break;
                case 7:
                    System.exit(0);
                default:
                    System.out.println("无效选择，请重新输入.");
            }
        }
    }
}
